package com.CST.controller.cb;

import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.dto.cb.BaseInComeDTO;
import com.CST.dto.cb.DateDTO;
import com.CST.dto.cb.InSourceDTO;
import com.CST.entity.cb.BaseInCome;
import com.CST.entity.cb.BdDepartment;
import com.CST.mapper.cst.cb.BaseInComeMapper;
import com.CST.mapper.dataplatform.BdDepartmentMapper;
import com.CST.service.cb.IBaseInComeService;
import com.CST.util.PageHelperTool;
import com.CST.vo.cb.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LairDa
 */
@RestController
@RequestMapping("/baseInCome")
@Api(tags = "BaseInCome")
public class BaseInComeController {

    @Autowired
    private IBaseInComeService baseInComeService;
    @Autowired
   private BaseInComeMapper baseInComeMapper;
    @Autowired
    private BdDepartmentMapper bdDepartmentMapper;

    @PostMapping("/test")
    @ApiOperation("test")
    public Response listInComeCoefficient(@RequestBody DateDTO dateDTO) {
        return baseInComeService.listInComeCoefficient(dateDTO);
    }

//    @PostMapping("/insertBaseInCome")
//    @ApiOperation("采集收入数据")
//    public Response insertBaseInCome(@RequestBody InSourceDTO inSourceDTO) {
//        baseInComeService.insertBaseInCome(inSourceDTO);
//        return Response.success("采集成功");
//    }

//    @PostMapping("/listBaseInCome")
//    @ApiOperation("获取收入数据")
//    public ResultObject listBaseInCome(@RequestBody InSourceDTO inSourceDTO) {
//        try {
//            List<InSourceVO> inSourceVOList = baseInComeService.listBaseInCome(inSourceDTO);
//            BigDecimal amount = BigDecimal.ZERO;
//            for (InSourceVO inSourceVO : inSourceVOList) {
//                amount = amount.add(inSourceVO.getAmount());
//            }
//            Map map = new HashMap();
//            map.put("data", PageHelperTool.excutePageInfo(inSourceVOList, inSourceDTO.getPageNum(), inSourceDTO.getPageSize()));
//            map.put("amount", amount);
//            return ResultObject.successReturn(map);
//        } catch (Exception e) {
////            e.printStackTrace();
//            return ResultObject.errorReturn("获取收入数据出错");
//        }
//    }

    @PostMapping("/addBaseInCome")
    @ApiOperation("添加收入数据")
    public ResultObject<Boolean> addBaseInCome(@RequestBody BaseInComeDTO dto) {
        return ResultObject.success(baseInComeService.addBaseInCome(dto));
    }

    @GetMapping("/remove/{id}/{year}")
    @ApiOperation("删除收入数据")
    public ResultObject<Boolean> remove(@PathVariable long id, @PathVariable int year) {
        return ResultObject.success(baseInComeService.remove(id, year));
    }

    /**
     * 科室收入项目分析报表
     * 按照父级科室编号+时间范围+开单or执行计算收入数据，并按照收入项目归档
     * dateDTO.unitCode = bill为开单
     * dateDTO.unitCode = exe为执行
     */
    @PostMapping("/listInComeByDateAndPDeptWithFee/{pDeptCode}")
    @ApiOperation("科室收入项目分析报表")
    public ResultObject listInComeByDateAndPDeptWith(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {
        List<BaseInCome> list = baseInComeService.listInComeByDateAndPDeptWith(dateDTO, pDeptCode);
        return ResultObject.successReturn(list);
    }



    @PostMapping("/listAllBaseCost")
    @ApiOperation("业务科室收支汇总表")
    public ResultObject listAllBaseCost(@RequestBody DateDTO dateDTO) {
        List<ReportAllBaseCostVO> reportAllBaseCostVOS = baseInComeService.listBaseCostAndInCome(dateDTO);
        return ResultObject.successReturn(reportAllBaseCostVOS);
    }
    @PostMapping("/listALLBaseCostByZDept")
    @ApiOperation("住院科室全成本表")
    public ResultObject listALLBaseCostByZDept(@RequestBody DateDTO dateDTO) {
        List<ReportAllBaseCostVO> reportAllBaseCostVOS = baseInComeService.listALLBaseCostByZDept(dateDTO);
        return ResultObject.successReturn(reportAllBaseCostVOS);
    }




    /**
     * 科室收入分析
     * 根据父级科室编号+时间范围计算其子科室的各项收入类型
     * 包括：
     * 1.开单收入
     * 2.本执本开
     * 3.他开本执
     * 1+3.合计收入
     * 2+3.小计
     */
    @PostMapping("/listInComeByDateAndPDeptWithAnalyse/{pDeptCode}")
    @ApiOperation("科室收入分析")
    public ResultObject listInComeByDateAndPDeptWithAnalyse(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {

        LambdaQueryWrapper<BdDepartment> bdDepartmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bdDepartmentLambdaQueryWrapper.eq(BdDepartment::getDeptCode,pDeptCode);
        BdDepartment bdDepartment = bdDepartmentMapper.selectOne(bdDepartmentLambdaQueryWrapper);
        List<ReportInComeAnalyseVO> list;
        if (bdDepartment.getParentCode().equals("1")){
            list = baseInComeService.listInComeByDateAndPDeptWithAnalyse(dateDTO, pDeptCode);
        }else {
            list = baseInComeMapper.listInComeByDateAndPDeptWithAnalyseNew(dateDTO, pDeptCode,null);
        }
        return ResultObject.successReturn(list);
    }

    @PostMapping("/listDeptProfitByDateAndPDept/{pDeptCode}")
    @ApiOperation("科室收益分析")
    public ResultObject listDeptProfitByDateAndPDept(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {
        List<ReportBusinessVO> reportBusinessVOList = baseInComeService.listDeptProfitByDateAndPDept(dateDTO, pDeptCode);
        return ResultObject.successReturn(reportBusinessVOList);
    }

    @PostMapping("/listDeptProfitCompositionByDateAndPDept/{pDeptCode}")
    @ApiOperation("科室构成分析")
    public ResultObject listDeptProfitCompositionByDateAndPDept(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {
        List<ReportBusinessVO> list = baseInComeService.listDeptProfitCompositionByDateAndPDept(dateDTO, pDeptCode);
        return ResultObject.successReturn(list);
    }

    /**
     * 科室收入项目分析明细
     * 传入时间+父级科室+项目编号
     * 查询明细
     * 通过unitCode控制开单和执行
     */
    @PostMapping("/listBaseInComeVOByDateAndPDeptGroupWithDeptAndFee/{pDeptCode}/{deptCode}/{feeCode}")
    @ApiOperation("科室收入项目分析明细")
    public ResultObject listBaseInComeVOByDateAndPDeptGroupWithDeptAndFee(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode, @PathVariable String deptCode, @PathVariable String feeCode) {
        System.out.println(feeCode);
        if ("null".equals(feeCode)) {
            feeCode = null;
        }
        if ("null".equals(pDeptCode)) {
            pDeptCode = null;
        }
        if ("null".equals(deptCode)) {
            deptCode = null;
        }
        List<BaseInComeVO> list = baseInComeService.listBaseInComeVOByDateAndPDeptGroupWithDeptAndFee(dateDTO, pDeptCode, deptCode, feeCode);
        return ResultObject.successReturn(list);
    }

    @PostMapping("/pointAnalysis/{pDeptCode}/{type}")
    @ApiOperation("保本点分析")
    public ResultObject pointAnalysis(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode, @PathVariable Integer type) {
        List<ReportPointVO> list = baseInComeService.pointAnalysis(dateDTO, pDeptCode, type);
        return ResultObject.successReturn(list);
    }


    /**
     * 重写的收入采集接口
     * xnd
     * @param inSourceDTO
     * @return
     */
    @PostMapping("/insertBaseInCome")
    @ApiOperation("采集收入数据")
    public Response insertBaseInCome(@RequestBody InSourceDTO inSourceDTO) throws InterruptedException {
        baseInComeService.insertBaseInComeNew(inSourceDTO);
        Thread.sleep(10000);
        return Response.success("收入采集成功");
    }

    /**
     * xnd
     * 重写获取收入数据
     * @param inSourceDTO
     * @return
     */
    @PostMapping("/listBaseInCome")
    @ApiOperation("获取收入数据")
    public ResultObject listBaseInCome(@RequestBody InSourceDTO inSourceDTO) {
        try {
            List<InSourceVO> inSourceVOList = baseInComeService.listBaseInCome(inSourceDTO);
            BigDecimal amount = BigDecimal.ZERO;
            for (InSourceVO inSourceVO : inSourceVOList) {
                amount = amount.add(inSourceVO.getAmount());
            }
            Map map = new HashMap();
            map.put("data", PageHelperTool.excutePageInfo(inSourceVOList, inSourceDTO.getPageNum(), inSourceDTO.getPageSize()));
            map.put("amount", amount);
            return ResultObject.successReturn(map);
        } catch (Exception e) {
//            e.printStackTrace();
            return ResultObject.errorReturn("获取收入数据出错");
        }
    }
    @PostMapping("/download")
    public void exportExcel(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        baseInComeService.exportExcel(response,dateDTO);
    }
    @PostMapping("/download1")
    public void exportExcel1(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        baseInComeService.exportExcel1(response,dateDTO);
    }
    @PostMapping("/test1")
    public ResultObject test(@RequestBody DateDTO dateDTO) {
        return ResultObject.successReturn(baseInComeService.test(dateDTO));
    }
}
